"""
异步函数示例
功能：演示如何在 Modal 中使用异步函数处理 I/O 密集型任务
特点：使用 async/await 语法，提高 I/O 操作效率
运行：modal run 16_async_function.py
"""
import modal
import asyncio

app = modal.App("async-app")
image = modal.Image.debian_slim().pip_install("httpx")

@app.function(image=image)
async def fetch_url(url):
    import httpx
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        return response.status_code

@app.local_entrypoint()
def main():
    urls = [
        "https://httpbin.org/status/200",
        "https://httpbin.org/status/404",
        "https://httpbin.org/status/500"
    ]
    
    # 并行获取多个URL的状态码
    results = list(fetch_url.map(urls))
    for url, status in zip(urls, results):
        print(f"{url}: {status}")